﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ChuanYe.CountyFireManagement.Api.Models;
using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ChuanYe.CountyFireManagement.Api.Controllers
{
    /// <summary>
    /// 火灾首报
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FireFirstReportController : ControllerBase
    {
        /// <summary>
        ///   增加
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase Add(FireFirstReportRequest obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            try
            {
                string ip = HttpContext.GetClientIp();
                Firefirstreport info = new Firefirstreport();
                info.FireId = obj.FireId;
                info.FirstReportContent = obj.FirstReportContent;
                info.AddTime = DateTime.Now;
                info.Ip = ip;
                info.TransDatetime = obj.TransDatetime.HasValue ? obj.TransDatetime.Value : DateTime.Now;
                info.UserId = obj.UserId;
                var data = BLL.FireFirstReportBLL.Instance.Add(info);
                if (data.FirstParam != null)
                {
                    ControllerHelper.SendMessage(data.FirstParam);
                    var allExpireFreInfo = BLL.FireExpireBLL.Instance.GetUnExpire();
                    MqttHub.fireexpires = allExpireFreInfo;
                }
                result.Status = data.Status;
                result.Message = data.Message;
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/FireFirstReport/Add Error";
                LogHelper.Error(result.Message,ex);
            }
            return result;
        }
    }
}